﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using YuShiManageSystem.DATA;
using YuShiManageSystem.COM;
using System.Threading;

namespace YuShiManageSystem.UI
{
    /// <summary>
    /// 登录窗体
    /// </summary>
    public partial class Login : Base
    {
        private readonly StoreService Service;

        public tb_store Store
        {
            get;
            private set;
        }

        public Login()
        {
            InitializeComponent();
        }

        public Login(Session se)
            : base(se)
        {
            InitializeComponent();
            this.Service = new StoreService(se.DataModel);
        }
        
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLogin_Click(object sender, EventArgs e)
        {
            //验证控件
            if (this.cmbStore.SelectedIndex == -1)                
                return;
            try
            {
                String errorText;
                //加密数据
                this.Store = this.Service.Login(this.cmbStore.Text, Encode.MD5.HashString(this.txtPwd.Text), out errorText);
                if (this.Store == null)
                {
                    this.errLabel.Text = errorText;
                    return;
                }
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (System.Data.EntityException )
            {
                YSMessage.Error("数据库连接失败！");
            }
            catch (Exception )
            {
                YSMessage.Error("登录失败,请联系管理员！");
            }
        }

        /// <summary>
        /// 门店名改变时清空密码和错误提示
        /// </summary>
        private void cmbStore_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.errLabel.Text=this.txtPwd.Text = "";
        }

        /// <summary>
		/// 加载门店列表
		/// </summary>
        private void login_Load(object sender, EventArgs e)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object obj)
                {
                    try
                    {
                        string[] userNameArr = this.Service.GetStoreNameArray();
                        MethodInvoker invoker = new MethodInvoker(delegate()
                            {
                                this.cmbStore.Items.Clear();
                                this.cmbStore.Items.AddRange(userNameArr);
                            });
                        this.ExecMethodInvoker(invoker);
                    }
                    catch (Exception)
                    {
                        YSMessage.Error("加载用户名列表失败，请检查数据库连接");
                    }
                }));
        }
    
        /// <summary>
		/// 在拥有此控件的基础窗口句柄的线程上执行指定的委托。（如果当前控件没有创建，则直接执行）
		/// </summary>
		/// <param name="methodInvoker"></param>
		public virtual void ExecMethodInvoker(MethodInvoker methodInvoker)
		{
			if (methodInvoker == null)
				return;
			if (this.Created)
				this.Invoke(methodInvoker);
			else
				methodInvoker();
		}

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtPwd_TextChanged(object sender, EventArgs e)
        {
            this.errLabel.Text = "";
        }
    }
}